let sdl_init () =
  begin
    Sdl.init [`EVERYTHING];
    Sdlevent.enable_events Sdlevent.all_events_mask;
  end

let get_dims img =
  ((Sdlvideo.surface_info img).Sdlvideo.w,
   (Sdlvideo.surface_info img).Sdlvideo.h)

  
(*i lgine, j colonne*)
    
(*on prend la matrice de convolution de taille 3 autour du pixel qu'on traite *)

let get_coord_color px str =
  match px with
    |(r,g,b) -> if str = "r" then  r
	  else if str = "g" then g
	  else b

(*si on atterit hors du cadre la case prend la valeur de la case du
  milieu*)
let val_pixelr  img f x y =
  let t = ref 0 in
  for i = -1 to 1 do
    for j= -1 to 1 do
      t := !t + (get_coord_color (Sdlvideo.get_pixel_color img (x+i) (y+j)) "r") *f.(i).(j) ;   
    done;
  done;
(!t/9)

let val_pixelg img f x y=
  let t = ref 0 in
  for i = -1 to 1 do
    for j= -1 to 1 do
      t := !t + (get_coord_color(Sdlvideo.get_pixel_color img (x+i) (y+j)) "g") *f.(i).(j) ;
    done;
  done;
(!t/9)

let val_pixelb  img f x y=
  let t = ref 0 in
  for i = -1 to 1 do
    for j= -1 to 1 do
      t := !t + (get_coord_color(Sdlvideo.get_pixel_color img (x+i) (y+j)) "b")*f.(i).(j);
    done;
  done;
(!t/9)


let traitement_image img f =
  let w = (Sdlvideo.surface_info img).Sdlvideo.w in  let h = (Sdlvideo.surface_info img).Sdlvideo.h in
  let img_f = Sdlvideo.create_RGB_surface_format img [] w h in
  for x = 0 to w do
      for y = 0 to h do
	let px_f = (val_pixelr img f x y,val_pixelg img f x y,val_pixelb img f x y) in
    Sdlvideo.put_pixel_color img_f x y px_f;
    done;
  done;
img_f




let moyenne() =
    Array.make_matrix 3 3 1


let moy_filt img  = 
      traitement_image img (moyenne())
  


(*let moy =
  let mat =Array.make
  mat(-1)(-1)


mat



  image img filt



  let moy_filt img = image img moy

*)
